package com.th.supcom.pds.deptdispensary.client.widget;

import java.util.List;

import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.th.supcom.framework.dict.web.gwt.client.model.DictModel;
import com.th.supcom.framework.organization.web.gwt.client.model.OrganizationModel;
import com.th.supcom.pds.common.client.model.CodeConstants;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.deptdispensary.client.model.DeptVsDispensaryModel;
import com.th.supcom.pds.deptdispensary.client.service.DeptVsDispensaryRpc;
import com.th.supcom.pds.deptdispensary.client.service.DeptVsDispensaryRpcAsync;

/**
 * 护理单元药房Action
 * 
 * @author 杨冲
 */
public class NurseVsDispensaryAction
{
    private DeptVsDispensaryFields m_fields;
    private NurseVsDispensaryGrid m_grid;
    private NurseVsDispensaryEditWindow m_window;
    private DeptVsDispensaryRpcAsync s_deptVsDispensaryRpcAsync = GWT.create (DeptVsDispensaryRpc.class);

    public NurseVsDispensaryAction ()
    {

    }

    /**
     * 设置护理单元药房文本组件
     * 
     * @param fields
     */
    public void setFields (DeptVsDispensaryFields fields)
    {

        this.m_fields = fields;
    }

    /**
     * 设置Grid
     * 
     * @param grid
     */
    public void setGrid (NurseVsDispensaryGrid grid)
    {

        m_grid = grid;
    }

    public void setWindow (NurseVsDispensaryEditWindow window)
    {

        m_window = window;
    }

    public void setS_deptVsDispensaryRpcAsync (DeptVsDispensaryRpcAsync s_deptVsDispensaryRpcAsync)
    {

        this.s_deptVsDispensaryRpcAsync = s_deptVsDispensaryRpcAsync;
    }

    /**
     * 编辑护理单元药房配置关系的时候初始化数据
     */
    public void initDataWhenEdit ()
    {
        DeptVsDispensaryModel model = m_grid.getGrid ().getSelectionModel ().getSelectedItem ();
        if (null == model)
        {
            return;
        }

        Long id = model.getDeptVsDispensaryId ();

        s_deptVsDispensaryRpcAsync.getDeptVsDispensaryById (id, new AsyncCallback <DeptVsDispensaryModel> ()
        {
            @Override
            public void onSuccess (DeptVsDispensaryModel result)
            {
                initDataWhenEdit (result);
            }

            @Override
            public void onFailure (Throwable caught)
            {
                MessageBox.alert (CodeConstants.PROMPT, caught.getLocalizedMessage (), null);
            }
        });
    }

    /**
     * 编辑的时候初始化数据
     */
    private void initDataWhenEdit (DeptVsDispensaryModel model)
    {

        OrganizationModel dispensaryModel = new OrganizationModel ();
        dispensaryModel.setCode (model.getDispensary ());
        dispensaryModel.setName (model.getDispensaryName ());
        m_fields.getDispensaryBox ().setValue (dispensaryModel);

        DictModel withdrawTypeModel = new DictModel ();
        withdrawTypeModel.setDictItemCode (model.getWithdrawType ());
        withdrawTypeModel.setDictItemName (model.getWithdrawTypeName ());
        m_fields.getWithdrawTypeBox ().setValue (withdrawTypeModel);

        OrganizationModel deptModel = new OrganizationModel ();
        deptModel.setCode (model.getDeptCode ());
        deptModel.setName (model.getDeptName ());
        m_fields.getDeptBox ().setValue (deptModel);

        DictModel shiftModel = new DictModel ();
        shiftModel.setDictItemCode (model.getShift ());
        shiftModel.setDictItemName (model.getShiftName ());
        m_fields.getShiftBox ().setValue (shiftModel);

        DictModel isReturn = new DictModel ();
        isReturn.setDictItemCode (model.getIsReturn () == null ? null : model.getIsReturn ().toString ());
        isReturn.setDictItemName (model.getIsReturnName ());
        m_fields.getIsReturnBox ().setValue (isReturn);

    }

    /**
     * 获取护理单元药房配置关系
     * 
     * @return
     */
    private DeptVsDispensaryModel getDeptVsDispensaryModel (boolean isEdit)
    {
        DeptVsDispensaryModel model = new DeptVsDispensaryModel ();
        if (isEdit)
        {
            model.setDeptVsDispensaryId (m_grid.getGrid ().getSelectionModel ().getSelectedItem ()
                                               .getDeptVsDispensaryId ());
        }
        model.setDeptCode (m_fields.getDeptBox ().getValue ().getCode ());
        model.setDispensary (m_fields.getDispensaryBox ().getValue ().getCode ());
        model.setIsReturn (Integer.parseInt (m_fields.getIsReturnBox ().getValue ().getDictItemCode ()));
        return model;
    }

    /**
     * 新增护理单元药房配置关系
     * 
     * @param model
     */
    private void addDeptVsDispensary (final boolean isAddContinue)
    {
        DeptVsDispensaryModel model = getDeptVsDispensaryModel (false);
        s_deptVsDispensaryRpcAsync.addDeptVsDispensary (model, new AsyncCallback <Void> ()
        {
            @Override
            public void onFailure (Throwable caught)
            {
                MessageBox.alert (CodeConstants.PROMPT, caught.getLocalizedMessage (), null);
            }

            @Override
            public void onSuccess (Void result)
            {
                Info.display (CodeConstants.PROMPT, "添加成功");
                refresh ();
                if (isAddContinue)
                {
                    clearEditWindow ();
                }
                else
                {
                    m_window.hide ();
                }
            }
        });
    }

    /**
     * 编辑护理单元药房关系
     */
    private void editDeptVsDispensary ()
    {
        DeptVsDispensaryModel model = getDeptVsDispensaryModel (true);
        model.setWithdrawType (m_fields.getWithdrawTypeBox ().getValue ().getDictItemValue ());
        s_deptVsDispensaryRpcAsync.modifyPhamMeterialTypeByExample (model, new AsyncCallback <Void> ()
        {
            @Override
            public void onFailure (Throwable caught)
            {
                MessageBox.alert (CodeConstants.PROMPT, caught.getLocalizedMessage (), null);
            }

            @Override
            public void onSuccess (Void result)
            {
                Info.display (CodeConstants.PROMPT, "更新成功");
                refresh ();
                m_window.hide ();
            }
        });
    }

    /**
     * 编辑护理单元药房关系
     * 
     * @param isEdit
     */
    public void editDeptVsDispensary (boolean isEdit, boolean isAddContinue)
    {
        if (!validate ())
        {
            return;
        }
        if (isEdit)
        {
            editDeptVsDispensary ();
        }
        else
        {
            addDeptVsDispensary (isAddContinue);
        }
        return;
    }

    /**
     * 清除编辑窗口
     */
    public void clearEditWindow ()
    {
        m_fields.getDispensaryBox ().clear ();
        m_fields.getDeptBox ().clear ();
        m_fields.getPriorityBox ().clear ();
        m_fields.getShiftBox ().clear ();
        m_fields.getIsReturnBox ().clear ();
    }

    /**
     * 刷新数据
     */
    public void refresh ()
    {
        DeptVsDispensaryModel model = new DeptVsDispensaryModel ();
        String dispensary = ClientUtils.getOrgModel ().getOrgCode ();
        model.setDispensary (dispensary);
        s_deptVsDispensaryRpcAsync.getDeptVsDispensaryByExample (model,
                                                                 new AsyncCallback <List <DeptVsDispensaryModel>> ()
                                                                 {
                                                                     @Override
                                                                     public void onFailure (Throwable caught)
                                                                     {
                                                                         MessageBox.alert (CodeConstants.PROMPT,
                                                                                           caught.getLocalizedMessage (),
                                                                                           null);
                                                                     }

                                                                     @Override
                                                                     public void onSuccess (List <DeptVsDispensaryModel> result)
                                                                     {
                                                                         m_grid.getGrid ().getStore ().removeAll ();
                                                                         m_grid.getGrid ().getStore ().add (result);
                                                                     }
                                                                 });
    }

    /**
     * 验证组件是否为空
     * 
     * @return
     */
    private boolean validate ()
    {
        if (null == m_fields.getDispensaryBox ().getValue ())
        {
            MessageBox.alert (CodeConstants.PROMPT, "药房不能为空", null);
            return false;
        }

        if (null == m_fields.getDeptBox ().getValue ())
        {
            MessageBox.alert (CodeConstants.PROMPT, "科室不能为空", null);
            return false;
        }
        if (null == m_fields.getWithdrawTypeBox ().getValue ())
        {
            MessageBox.alert (CodeConstants.PROMPT, "退药方式不能为空", null);
            return false;
        }
        if (null == m_fields.getIsReturnBox ().getValue ())
        {
            MessageBox.alert (CodeConstants.PROMPT, "是否强退字段不能为空", null);
            return false;
        }
        return true;
    }

    /**
     * 判断Grid是否选中
     * 
     * @return
     */
    public boolean getGridSelectedStatus ()
    {
        DeptVsDispensaryModel model = m_grid.getGrid ().getSelectionModel ().getSelectedItem ();
        if (null == model)
        {
            return false;
        }
        return true;

    }

    /**
     * 删除选中的内容
     */
    public void delete ()
    {
        DeptVsDispensaryModel model = m_grid.getGrid ().getSelectionModel ().getSelectedItem ();
        Long id = model.getDeptVsDispensaryId ();
        s_deptVsDispensaryRpcAsync.removeDeptVsDispensary (id, new AsyncCallback <Void> ()
        {

            @Override
            public void onFailure (Throwable caught)
            {
                MessageBox.alert (CodeConstants.PROMPT, caught.getLocalizedMessage (), null);
            }

            @Override
            public void onSuccess (Void result)
            {
                Info.display (CodeConstants.PROMPT, "删除成功");
                refresh ();
            }
        });
    }

}
